import axios from "../../http/axios.js";
import getServiceList, {
  getServiceByHospitalId
} from "../../assets/js/service-list.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    noList: {
      img: "/assets/img/zwjl.png",
      text: "暂无服务"
    },
    whereComefrom: null, // 从上一个什么页面跳进来的 1 护理上门 2 院内护理 3首页tab医院列表
    hospitalId: 1, // 医院的唯一标识
    inputObj: {
      type: "text",
      placeholder: "输入要查找的服务",
      maxlength: 10,
      value: ""
    },
    serviceList: [
      // {
      //   id: 1,
      //   src: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1565785713630&di=1537bc03c22d0117a71c48b045d9cea7&imgtype=0&src=http%3A%2F%2Fa.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Ff603918fa0ec08fa3139e00153ee3d6d55fbda5f.jpg",
      //   title: "222",
      //   description: "伤口清洁，换敷料"
      // },
      // {
      //   id: 2,
      //   src: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1565785713630&di=1537bc03c22d0117a71c48b045d9cea7&imgtype=0&src=http%3A%2F%2Fa.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Ff603918fa0ec08fa3139e00153ee3d6d55fbda5f.jpg",
      //   title: "打针",
      //   description: "肌肉注射，皮下注射"
      // }
    ]
  },
  // 点击其中的一项item跳转到服务详情
  onTapOne: function(e) {
    let detail = e.detail;
    let item = detail.item;
    let index = detail.index;
    // console.log(item);
    //此处可能根据具体从哪个页面跳进来的去跳页面
    let whereComefrom = this.data.whereComefrom;
    if (whereComefrom == 1 || whereComefrom == 2) { //护理上门来的
      wx.navigateTo({ // 先选择了服务选择号医院再去跳详情
        url: `/pages/u_serviceHospital/u_serviceHospital?serviceId=${item.id}&title=${item.title}`
      })
    } else { //医院列表来的 // 先选择了医院服务选择好就可以直接跳详情
      wx.navigateTo({
        url: `/pages/u_detail/u_detail?serviceId=${item.id}&hospitalId=${this.data.hospitalId}&title=${item.title}`
      })
    }
  },
  // 搜索查找服务
  input: function(e) {
    let value = e.detail;
    let inputObj = this.data.inputObj;
    inputObj.value = value;
    this.setData({
      inputObj
    })
    this.getList();
  },
  onLoad: function(options) {
    let nursingType = options.nursingType;
    let title = options.title;
    wx.setNavigationBarTitle({
      title
    })
    if (nursingType) { // 从首页的home栏目中的护理类型跳来的
      if (nursingType == "护理上门") { // 当是护理上门的时候
        // 根据类型获取对应的服务列表
        this.setData({
          whereComefrom: 1
        })
      } else { // 当是院内护理的时候
        this.setData({
          whereComefrom: 2
        })
      }
    } else { // 从首页的医院栏目选中医院后跳进来的
      let hospitalId = options.hospitalId;
      this.setData({
        whereComefrom: 3,
        hospitalId: hospitalId
      })
    }
    this.getList();
  },
  onReady: function() {},

  onShow: function() {},
  // 获取相应的服务列表  等待调接口
  getList: function() {
    let type = null;
    let {
      whereComefrom
    } = this.data;
    if (whereComefrom == 1) { //护理上门 
      type = 1;
      getServiceList({
        page: 1,
        pageSize: 1000,
        name: this.data.inputObj.value,
        type
      }).then(res => {
        let serviceList = [];
        if (res.records.length) {
          res.records.forEach((item, index) => {
            let item1 = item;
            item1.title = item.name;
            item1.id = item.itemId; //唯一标识
            item1.src = item.image; //图片
            item1.description = item.description; //简短服务描述
            serviceList.push(item1);
          })
        } else {
          serviceList = [];
        }
        this.setData({
          serviceList
        })
      })
    } else if (whereComefrom == 2) { //院内护理
      type = 2;
      getServiceList({
        page: 1,
        pageSize: 1000,
        name: this.data.inputObj.value,
        type
      }).then(res => {
        let serviceList = [];
        if (res.records.length) {
          res.records.forEach((item, index) => {
            let item1 = item;
            item1.title = item.name;
            item1.id = item.itemId; //唯一标识
            item1.src = item.image; //图片
            item1.description = item.description; //简短服务描述
            serviceList.push(item1);
          })
        } else {
          serviceList = [];
        }
        this.setData({
          serviceList
        })
      })
    } else {
      type = null;
      getServiceByHospitalId({
        page: 1,
        pageSize: 1000,
        name: this.data.inputObj.value,
        hospitalId: this.data.hospitalId
      }).then(res => {
        let serviceList = [];
        if (res.records.length) {
          res.records.forEach((item, index) => {
            let item1 = item;
            item1.title = item.name;
            item1.id = item.id; //唯一标识
            item1.src = item.image; //图片
            item1.description = item.description; //简短服务描述
            serviceList.push(item1);
          })
        } else {
          serviceList = [];
        }
        this.setData({
          serviceList
        })
      })
    }
  }
})